package com.project.modules.notice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.common.api.CommonPage;
import com.project.common.api.CommonResult;
import com.project.modules.notice.model.TblGroup;
import com.project.modules.notice.service.TblGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Controller
 *
 * @author Qing2514
 */
@RestController
@Api(value = "TblGroupController", tags = "管理")
@RequestMapping("/group")
public class TblGroupController {

    @Autowired
    private TblGroupService groupService;

    @ApiOperation("根据ID查询")
    @GetMapping("/{id}")
    public CommonResult<TblGroup> getById(@PathVariable Long id) {
        TblGroup group = groupService.getById(id);
        return CommonResult.success(group);
    }

    @ApiOperation("【根据user_id查询】")
    @GetMapping("/{userId}")
    public CommonResult<List<TblGroup>> getByUserId(@PathVariable Long userId) {
        final List<TblGroup> list = groupService.list(new LambdaQueryWrapper<TblGroup>().eq(TblGroup::getUserId, userId));
        return CommonResult.success(list);
    }

    @ApiOperation("根据关键字分页模糊查询")
    @GetMapping(value = "/page")
    public CommonResult<CommonPage<TblGroup>> getPage(@RequestParam(value = "keyword", required = false) Long keyword,
                                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<TblGroup> list = groupService.getPage(keyword, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("添加")
    @PostMapping("")
    public CommonResult<Object> create(@RequestBody TblGroup group) {
        boolean success = groupService.save(group);
        return success ? CommonResult.success() : CommonResult.failed();
    }

    @ApiOperation("修改")
    @PutMapping("")
    public CommonResult<Object> update(@RequestBody TblGroup group) {
        boolean success = groupService.updateById(group);
        return success ? CommonResult.success() : CommonResult.failed();
    }

    @ApiOperation("根据ID删除")
    @DeleteMapping("/{id}")
    public CommonResult<Object> delete(@PathVariable Long id) {
        boolean success = groupService.removeById(id);
        return success ? CommonResult.success() : CommonResult.failed();
    }

}
